﻿
$(function () {
    var showErrors = function (element, message) {
        element.qtip({
            content: message,
            show: { event: false, ready: true },
            hide: { event: 'keydown' },
            position: {
                my: 'center left',
                at: 'center right'
            },
            style: {
                classes: 'qtip-red'
            }
        });
    };

    $.each($('form'), function () {
        var settings = $.data(this, 'validator').settings;
        var oldErrorFunction = settings.errorPlacement;
        var oldSucessFunction = settings.success;
        settings.errorPlacement = function (error, inputElement) {
            //oldErrorFunction(error, inputElement);
            showErrors($(inputElement), error.text());
        };
        settings.success = function (error) {
            oldSucessFunction(error);
        };
    });

    $('.field-validation-error').each(function () {
        // Get the name of the element the error message is intended for
        // Note: ASP.NET MVC replaces the '[', ']', and '.' characters with an
        // underscore but the data-valmsg-for value will have the original characters

        //var inputElem = '#' + $(this).attr('data-valmsg-for').replace('.', '_').replace('[', '_').replace(']', '_');
        var inputElem = '[name="' + $(this).attr('data-valmsg-for').replace('.', '_').replace('[', '_').replace(']', '_') + '"]';

        var corners = ['left center', 'right center'];
        var flipIt = $(inputElem).parents('span.right').length > 0;

        // Hide the default validation error
        $(this).hide();

        var message = $(this).text();
        $(inputElem).filter(':not(.valid)').each(function () {
            showErrors($(this), message);
        });
    });
});